{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "40e14358-9ab6-4190-93af-0fcb48b54389",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.makedirs(os.path.join('..', 'data'), exist_ok=True)\n",
    "data_file = os.path.join('..', 'data', 'house_tiny.csv')\n",
    "with open(data_file, 'w') as f:\n",
    "    f.write('NumRooms,Alley,Price\\n')  # 列名\n",
    "    f.write('NA,Pave,127500\\n')  # 每行表示一个数据样本\n",
    "    f.write('2,NA,106000\\n')\n",
    "    f.write('4,NA,178100\\n')\n",
    "    f.write('NA,NA,140000\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1825ab41-ae93-456b-8f81-21db024211af",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "09551f8b-0235-4e06-b85b-f2db9115834c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   NumRooms Alley   Price\n",
      "0       NaN  Pave  127500\n",
      "1       2.0   NaN  106000\n",
      "2       4.0   NaN  178100\n",
      "3       NaN   NaN  140000\n"
     ]
    }
   ],
   "source": [
    "data = pd.read_csv(data_file)\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d7a46f04-4ea0-4caa-87e9-191bf9b73ea2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                            NumRooms  \\\n",
      "0  <bound method NDFrame._add_numeric_operations....   \n",
      "1                                                2.0   \n",
      "2                                                4.0   \n",
      "3  <bound method NDFrame._add_numeric_operations....   \n",
      "\n",
      "                                               Alley  \n",
      "0                                               Pave  \n",
      "1  <bound method NDFrame._add_numeric_operations....  \n",
      "2  <bound method NDFrame._add_numeric_operations....  \n",
      "3  <bound method NDFrame._add_numeric_operations....  \n"
     ]
    }
   ],
   "source": [
    "inputs, outputs = data.iloc[:, 0:2], data.iloc[:,2]\n",
    "inputs = inputs.fillna(inputs.mean)\n",
    "print(inputs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ea3e9b34-ef3b-4b1a-abd0-e15149b1f1cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   NumRooms_<bound method NDFrame._add_numeric_operations.<locals>.mean of    NumRooms Alley\\n0       NaN  Pave\\n1       2.0   NaN\\n2       4.0   NaN\\n3       NaN   NaN>  \\\n",
      "0                                               True                                                                                                                        \n",
      "1                                              False                                                                                                                        \n",
      "2                                              False                                                                                                                        \n",
      "3                                               True                                                                                                                        \n",
      "\n",
      "   NumRooms_2.0  NumRooms_4.0  NumRooms_nan  \\\n",
      "0         False         False         False   \n",
      "1          True         False         False   \n",
      "2         False          True         False   \n",
      "3         False         False         False   \n",
      "\n",
      "   Alley_<bound method NDFrame._add_numeric_operations.<locals>.mean of    NumRooms Alley\\n0       NaN  Pave\\n1       2.0   NaN\\n2       4.0   NaN\\n3       NaN   NaN>  \\\n",
      "0                                              False                                                                                                                     \n",
      "1                                               True                                                                                                                     \n",
      "2                                               True                                                                                                                     \n",
      "3                                               True                                                                                                                     \n",
      "\n",
      "   Alley_Pave  Alley_nan  \n",
      "0        True      False  \n",
      "1       False      False  \n",
      "2       False      False  \n",
      "3       False      False  \n"
     ]
    }
   ],
   "source": [
    "inputs = pd.get_dummies(inputs, dummy_na= True)\n",
    "print(inputs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4cc26da0-fc2c-4c0e-a2d4-44dc8cc40e6c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1., 0., 0., 0., 0., 1., 0.],\n",
      "        [0., 1., 0., 0., 1., 0., 0.],\n",
      "        [0., 0., 1., 0., 1., 0., 0.],\n",
      "        [1., 0., 0., 0., 1., 0., 0.]], dtype=torch.float64)\n",
      "tensor([127500., 106000., 178100., 140000.], dtype=torch.float64)\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "X = torch.tensor(inputs.to_numpy(dtype=float))\n",
    "Y = torch.tensor(outputs.to_numpy(dtype=float))\n",
    "print(X)\n",
    "print(Y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a047bd5d-c9bf-445d-bbb8-86fcd42130db",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
